package com.smartmail.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.ArrayAdapter;

import com.smartmail.accounts.MailAccount;
import com.smartmail.mailing.MailMessage;
import com.smartmail.receiving.IncomingMailDBManager;
import com.smartmail.receiving.Receiving;
import com.smartmail.utility.MailMessageComparator;

/**
 * Receiver for intents sent from ReceivingService
 * about new message arrival
 * 
 * this receiver is binded with intent from AbstractRetrievalThread._NEW_MAIL_ARRIVED_INTENT
 * 
 * @author Tomek
 *
 */
public class MessageInfoReceiver extends BroadcastReceiver {

	/**
	 * log tag used for logging purposes
	 */
	public static final String _logTag = "com.smartmail.activity";
	
	/**
	 * adapter of list of messages in AccountMsgActivity
	 */
	private ArrayAdapter<MailMessage> _adapter;
	
	private MailMessageComparator _mailMessageComparator;
	
	/**
	 * every instance of that receiver is binded with one 
	 * mail account
	 */
	private MailAccount _mailAccount;
	
	public MessageInfoReceiver(ArrayAdapter<MailMessage> adapter, MailAccount account){
		
		_adapter = adapter;
		_mailAccount = account;
		
		_mailMessageComparator = new MailMessageComparator();
		
	}
	
	/**
	 * when intent is sent to AccountMsgActivity this method is invoked
	 * 
	 * first - obtain from intent info about mail account binded to this message
	 * 			and obtain message id (globally unique) from intent
	 * second - retrieve from DB information about this message and send it to adapter
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
		
		int accountID = 
				intent.getIntExtra(Receiving.MAIL_INFO_ACCOUNT_ID,-1);
		String messageHash = 
				intent.getStringExtra(Receiving.MAIL_INFO_HASH);
		
		
		/**
		 * check if this message is sent to appropriate mail account
		 */
		if( _mailAccount.getAccountID() != accountID )
			return;
		
		String action = intent.getAction();
		if(action == null){
			Log.d(_logTag, "MessageInfoReceiver : intent action null specified");
			return;
		}
		
		
		/**
		 * retrieve message info from database
		 */
		IncomingMailDBManager _dbManager = Receiving.getDBManager(_mailAccount);
		MailMessage _newMessage = _dbManager.getMessage(messageHash);
		
		
		
		
		/**
		 * when completely new message arrived
		 */
		if( action.equals(Receiving.NEW_MESSAGE_ARRIVED_INTENT) ){
			
			/**
			 * delete old info about this message from adapter
			 */
			MailMessage toRemove = null;
			for(int i=0;i<_adapter.getCount();i++){
				
				MailMessage m = _adapter.getItem(i);
				if(m.getMessageHash().equals(messageHash)){
					toRemove = m;
					break;
				}
			}
			if(toRemove != null)
				_adapter.remove(toRemove);
			
			if(_newMessage != null){
				
				_adapter.add(_newMessage);
				_adapter.sort(_mailMessageComparator);
			}
		}
		/**
		 * when message is downloaded from server (POP3 case)
		 */
		else if( action.equals(Receiving.MESSAGE_DOWNLOADED_INTENT) ){
			
			/**
			 * delete old info about this message from adapter
			 */
			MailMessage toRemove = null;
			for(int i=0;i<_adapter.getCount();i++){
				
				MailMessage m = _adapter.getItem(i);
				if(m.getMessageHash().equals(messageHash)){
					toRemove = m;
					break;
				}
			}
			if(toRemove != null)
				_adapter.remove(toRemove);
			
			if(_newMessage != null){
				
				_adapter.add(_newMessage);
				_adapter.sort(_mailMessageComparator);
			}
		}
		/**
		 * when message is deleted from application
		 */
		else if( action.equals(Receiving.MESSAGE_DELETED_INTENT)){
			
			MailMessage toRemove = null;
			
			int len = _adapter.getCount();
			for(int i=0;i<len;i++){
				
				MailMessage m = _adapter.getItem(i);
				if( m.getMessageHash().equals(messageHash)){
					
					toRemove = m;
					break;
				}
			}
			
			if(toRemove != null)
				_adapter.remove(toRemove);
		} 
		else if( action.equals((Receiving.MESSAGE_UNAVAILABLE_INTENT))){
				
			int len = _adapter.getCount();
			for(int i=0;i<len;i++){
				
				MailMessage m = _adapter.getItem(i);
				if( m.getMessageHash().equals(messageHash)){
					
					m.setState(MailMessage._MESSAGE_NOT_DOWNLOADED);
					break;
				}
			}
			
			
		}
		
		
	}

}
